/**
 * Description:该类主要用于创建一个视频播放控件
 * Copyright: 成都和为科技有限公司 2012
 * 	   属性：
 * 		  - src 
 *     事件：
 *        - onplay  开始播放
 *        - onomplete 完成播放
 *        - onpause 暂停
 *        - onvolume 声音改变
 */
function FlvPlayer() {}

FlvPlayer.inherit(Control);

/**
 * 初始化方法
 * @param {Object} g_sPath
 * @param {Object} sName 控件名称
 * @param {Object} obj	控件html对象	
 * @param {Object} parent 父控件
 * @param {Object} oAttr 属性对象
 */
FlvPlayer.prototype.init = function () {
	FlvPlayer.base.init.call(this);
}

/**
 * 创建HTML节点
 */
FlvPlayer.prototype.create = function () {
	if(!this._html){
		this._html = document.createElement("div");
		this._id = Math.uuid();
		$(this.getHTML()).append("<div id='" + this._id + "' style='width:100%;height:100%'></div>")
	}
}

FlvPlayer.prototype.initLoad = function() {
	try {
		$f
	} catch (e) {
		this.loadScriptFile("/public.common/webapi/common/flowplayer/flowplayer-3.2.13.js");
	}
	if (this.getAttribute().src) {
		var src = this.getComponent().getAbsolutePath(this.getAttribute().src);
		this.setSrc(src);
	}
}

/**
 * 设置播放文件路劲
 */
FlvPlayer.prototype.setSrc = function(src){
	this._src = src;
	this._palyer = $f(this._id, "/public.common/webapi/common/flowplayer/flowplayer-3.2.18.swf", 
		this.getSetting(src));
//	if (!jwplayer(this._id)) return;
//	this._jwpalyer = jwplayer(this._id).setup({
//		flashplayer:"/public.common/webapi/common/jwplayer/jwplayer.flash.swf",
//		file:src,
//		height:$(this.getHTML()).height(),
//		width:$(this.getHTML()).width()
//	});
//	var that = this;
//	this._jwpalyer.onPlay(function(){
//		that.triggerAttributeEvent("onplay");
//	});
//	this._jwpalyer.onPause(function(){
//		that.triggerAttributeEvent("onpause");
//	});
//	this._jwpalyer.onComplete(function(){
//		that.triggerAttributeEvent("oncomplete");
//	});
//	this._jwpalyer.onVolume(function(){
//		that.triggerAttributeEvent("onvolume");
//	});
}


FlvPlayer.prototype.getSetting = function(src) {
	var setting = {};
	setting.clip = {};
	setting.plugins = {};
	if (this.isMusic(src)) {
		setting.clip.provider = "audio";
		setting.plugins.audio = {url:"/public.common/webapi/common/flowplayer/flowplayer.audio-3.2.11.swf"};
		setting.plugins.controls = {fullscreen:false,height:30,autoHide:false};
	}
	setting.clip.url = src;

    var that = this;
    setting.clip.onStart = function(){
        //alert("start");
        that.triggerAttributeEvent("onstart");
    }
    setting.clip.onPause = function(){
        //alert("onPause");
        that.triggerAttributeEvent("onpause");
    }
    setting.clip.onResume = function(){
        //alert("onResume");
        that.triggerAttributeEvent("onresume");
    }
    setting.clip.onFinish = function(){
        //alert("onFinish");
        that.triggerAttributeEvent("onfinish");
    }
    setting.clip.onBegin = function(){
        //alert("onResume");
        that.triggerAttributeEvent("onbegin");
    }
    
	return setting;
}

FlvPlayer.prototype.isMusic = function(src) {
	var musics = [".mp3",".MP3",".wav",".WAV"];
	for (var i = 0 ; i < musics.length ; i++) {
		if (src.lastIndexOf(musics[i]) != -1 ) {
			return true;
		}
	}
	return false;
}

	
/**
 * 开始播放
 */
FlvPlayer.prototype.play = function() {
	this._palyer.play();
}

/**
 * 暂停
 */
FlvPlayer.prototype.pause = function() {
	this._palyer.pause();
}

/**
 * 停止播放
 */
FlvPlayer.prototype.stop = function() {
	this._palyer.stop();
}

FlvPlayer.prototype.setValue = function(value) {
	this.setSrc(value);
}

FlvPlayer.prototype.getValue = function() {
	return this._src;
}

/**
 * 销毁播放器
 */
FlvPlayer.prototype.destroyPlayer = function () {
	this._html.parentNode.removeChild(this._html);
}